package com.A21.Adapter;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.A21.Object.MSTCustomer;
import com.A21MobileSales.CustomerInfo;
import com.A21MobileSales.CustomerProduct;
import com.A21MobileSales.CustomerSOA;
import com.A21MobileSales.InvoiceHistory;
import com.A21MobileSales.R;

public class CustomerSwipeListAdapter extends BaseAdapter {
	ViewHolder holder;
	LayoutInflater inflater;
	ArrayList<MSTCustomer> CustomerList;
	ArrayList<MSTCustomer> mOriginalValues;
	static final String KEY_CCode = "CCode";
	static final String KEY_CName = "CName";
	private Context context;

	public CustomerSwipeListAdapter() {
		// TODO Auto-generated constructor stub
	}

	public CustomerSwipeListAdapter(Context _context, ArrayList<MSTCustomer> map) {
		Activity act = (Activity) _context;
		this.context = _context;
		this.CustomerList = map;
		this.mOriginalValues = new ArrayList<MSTCustomer>();
		this.mOriginalValues.addAll(CustomerList);
		inflater = (LayoutInflater) act
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return CustomerList.size();
	}

	public Object getItem(int arg0) {
		return CustomerList.get(arg0);
	}

	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.row_swipe_customerselection, null);
			holder = new ViewHolder();
			holder.txtCCode = (TextView) vi
					.findViewById(R.id.CSLT_txtCustomerCode);
			holder.txtCName = (TextView) vi
					.findViewById(R.id.CSLT_txtCUstomerName);
			holder.btnInfo = (LinearLayout) vi.findViewById(R.id.btnInfo);
			holder.btnSOA = (LinearLayout) vi.findViewById(R.id.btnSOA);
			holder.btnPrice = (LinearLayout) vi.findViewById(R.id.btnPrice);
			holder.btnInvoice = (LinearLayout) vi.findViewById(R.id.btnInvoice);
			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}
		try {

			final MSTCustomer cust = CustomerList.get(position);
			holder.txtCCode.setText(cust.Code);

			// String strEncoded="";
			if (cust.CName == null) {
				cust.CName = "";
			}
			// strEncoded=new
			// String(cust.CName.getBytes(Charset.forName("iso-8859-1")),
			// Charset.forName("UTF-8"));

			// holder.txtCName.setText(strEncoded);
			holder.txtCName.setText(cust.CName);
			holder.btnInfo.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, CustomerInfo.class);
					Bundle bundle = new Bundle();
					bundle.putString("Code", cust.Code);
					intent.putExtras(bundle);
					Activity activity = (Activity) context;
					activity.startActivity(intent);

				}
			});

			holder.btnSOA.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, CustomerSOA.class);
					Bundle bundle = new Bundle();
					bundle.putString("Code", cust.Code);
					bundle.putString("CName", cust.CName);
					intent.putExtras(bundle);
					Activity activity = (Activity) context;
					activity.startActivity(intent);
				}
			});

			holder.btnPrice.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					Intent intent = new Intent(context, CustomerProduct.class);
					Bundle bundle = new Bundle();
					bundle.putString("Code", cust.Code);
					bundle.putString("CName", cust.CName);
					intent.putExtras(bundle);
					Activity activity = (Activity) context;
					activity.startActivity(intent);

				}
			});

			holder.btnInvoice.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, InvoiceHistory.class);
					Bundle bundle = new Bundle();
					bundle.putString("title1", "Customer Invoice History");
					bundle.putString("title2", cust.CName);
					bundle.putString("function", "CI");
					bundle.putString("Other", cust.Code);
					intent.putExtras(bundle);
					Activity activity = (Activity) context;
					activity.startActivity(intent);

				}
			});

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return vi;
	}

	private class ViewHolder {
		TextView txtCCode;
		TextView txtCName;
		LinearLayout btnInfo;
		LinearLayout btnSOA;
		LinearLayout btnPrice;
		LinearLayout btnInvoice;
	}

	public void filter(String charText) {
		charText = charText.toLowerCase(Locale.getDefault());
		CustomerList.clear();
		if (charText.length() == 0) {
			CustomerList.addAll(mOriginalValues);
		} else {
			for (MSTCustomer pc : mOriginalValues) {
				final String Code = pc.Code;
				final String Name = pc.CName;
				if (Code.toLowerCase().contains(
						charText.toString().toLowerCase())
						|| Name.toLowerCase().contains(
								charText.toString().toLowerCase())) {
					CustomerList.add(pc);
				}
			}
		}
		notifyDataSetChanged();
	}
}